Java动态生成类以及动态添加属性

您所在的位置:网站首页 java 生成类 Java动态生成类以及动态添加属性

Java动态生成类以及动态添加属性

2024-07-10 08:35:54| 来源: 网络整理| 查看: 265

有个技术实现需求:动态生成类,其中类中的属性来自参数对象中的全部属性以及来自参数对象properties文件。

那么技术实现支持:使用CGLib代理。

具体的实现步骤:

1.配置Maven文件:

 

4.0.0 com.journey journey 1.0-SNAPSHOT cglib cglib 2.2.2

2.封装的cglib类

 

package com.journey; import net.sf.cglib.beans.BeanGenerator; import net.sf.cglib.beans.BeanMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * Created by little_eleventh_wolf on 2017/11/18. */ public class DynamicBean { private Object object = null; //动态生成的类 private BeanMap beanMap = null; //存放属性名称以及属性的类型 public DynamicBean() { super(); } public DynamicBean(Map propertyMap) { this.object = generateBean(propertyMap); this.beanMap = BeanMap.create(this.object); } /** * @param propertyMap * @return */ private Object generateBean(Map propertyMap) { BeanGenerator generator = new BeanGenerator(); Set keySet = propertyMap.keySet(); for(Iterator i = keySet.iterator(); i.hasNext(); ) { String key = (String) i.next(); generator.addProperty(key, (Class) propertyMap.get(key)); } return generator.create(); } /** * 给bean属性赋值 * @param property 属性名 * @param value 值 */ public void setValue(Object property, Object value) { beanMap.put(property, value); } /** * 通过属性名得到属性值 * @param property 属性名 * @return 值 */ public Object getValue(String property) { return beanMap.get(property); } /** * 得到该实体bean对象 * @return */ public Object getObject() { return this.object; } }

3.需求的实现类:

 

package com.journey; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.InputStream; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.Properties; import java.util.Set; /** * Created by little_eleventh_wolf on 2017/11/18. */ public class ClassUtil { private String filePath = "/config/"; //配置文件路径 public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public Object dynamicClass(Object object) throws Exception { HashMap returnMap = new HashMap(); HashMap typeMap = new HashMap(); //读取配置文件 Properties prop = new Properties(); String sourcepackage = object.getClass().getName(); String classname = sourcepackage.substring(sourcepackage.lastIndexOf(".") + 1); InputStream in = ClassUtil.class.getResourceAsStream(filePath + classname + ".properties"); prop.load(in); Set keylist = prop.stringPropertyNames(); Class type = object.getClass(); BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for(int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if(!propertyName.equals("class")) { Method readMethod = descriptor.getReadMethod(); Object result = readMethod.invoke(object, new Object[0]); if(result != null) { returnMap.put(propertyName, result); } else { returnMap.put(propertyName, ""); } typeMap.put(propertyName, descriptor.getPropertyType()); } } //加载配置文件中的属性 Iterator iterator = keylist.iterator(); while(iterator.hasNext()) { String key = iterator.next(); returnMap.put(key, prop.getProperty(key)); typeMap.put(key, Class.forName("java.lang.String")); } //map转换成实体对象 DynamicBean bean = new DynamicBean(typeMap); //赋值 Set keys = typeMap.keySet(); for(Iterator it = keys.iterator(); it.hasNext(); ) { String key = (String) it.next(); bean.setValue(key, returnMap.get(key)); } Object obj = bean.getObject(); return obj; } public static void main(String[] args) throws Exception { new ClassUtil().dynamicClass(new LeapRole()/*LeapRole是个普通类,未贴源码*/); } }

4.技术实现目的:前台框架表格数据源实际上就是带有数据的实体,但是grid中数据的类型、以及是否可见、toolbar工具栏上的按钮、是否分页,是针对实体而言,所以目前把这些信息作为实体的配置文件。在展示页面之前,读取全部信息,转为参数对象的完整对象。



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭